﻿using DevExpress.XtraGrid.Views.Grid;
using RecruitStudents.Client.BLL;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecruitStudents.Client.Appointment
{
    public partial class Appointment_MakeAppointmentForm_Third_List : BaseForm
    {
        ServiceAppointment service = new ServiceAppointment();
        public Appointment_MakeAppointmentForm_Third_List()
        {
            InitializeComponent();
            if (SessionerHelper.ExamLevel == "1")
            {
                DevHelper.ShowErrorMessageBox(SessionerHelper.ExamLevelName + "级别不用进行手动分组!");
            }
            else
            {
                BindGrid_DFP();
                BindGroup();
                BindGrid_YFP();
            }
        }

        private void BindGroup()
        {
            //DataTable dtGroup = service.GetAppointmentGroupDataTable_Third(SessionerHelper.ExamPlaceID, SessionerHelper.SpecialityKindsNO, SessionerHelper.ExamLevel);
            //gdAppointment.DataSource = dtGroup;
        }

        private void BindGrid_DFP()
        {
            //gdAppointmentStudents.DataSource = service.GetList("", SessionerHelper.ExamLevel, SessionerHelper.ExamPlaceID, SessionerHelper.SpecialityKindsNO);
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (SessionerHelper.ExamLevel == "1")
            {
                DevHelper.ShowErrorMessageBox(SessionerHelper.ExamLevelName + "级别不用进行手动分组!");
                return;
            }
            BindGrid_YFP();
        }

        private string GetGroupNO()
        {
            string strSpecialityKindsNo_Detail = string.Empty;
            GridView rowsSelected = gridView1 as GridView;
            if (rowsSelected.Rows.Count > 0)
            {
                int[] inteurm = rowsSelected.GetSelectedRows();
                if (inteurm != null && inteurm.Length > 0)
                {
                    strSpecialityKindsNo_Detail = rowsSelected.Rows[inteurm[0]].DataRow["GroupNo"].ToString();
                }
            }
            return strSpecialityKindsNo_Detail;
        }

        private void BindGrid_YFP()
        {
            gdAppointmentStudents_YFP.DataSource = null;
            string strGroupNo = GetGroupNO();
            if (strGroupNo.Length > 0)
            {
                //using (DataTable dt = service.GetBindGridYFP(strGroupNo, SessionerHelper.SpecialityKindsNO, SessionerHelper.ExamPlaceID, SessionerHelper.ExamLevel))
                //{
                //    gdAppointmentStudents_YFP.DataSource = dt;
                //}
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (SessionerHelper.ExamLevel == "1")
            {
                DevHelper.ShowErrorMessageBox(SessionerHelper.ExamLevelName + "级别不用进行手动分组!");
                return;
            }
            string strGroupNo = GetGroupNO();
            if (strGroupNo.Length == 0)
            {
                DevHelper.ShowErrorMessageBox("请先选择考试组别!");
                return;
            }
            int userCount = 0;
            Dictionary<string, string> dic = new Dictionary<string, string>();

            GridRowCollection gridrows = gridView2.SelectedRows;
            if (gridrows == null || gridrows.Count == 0)
            {
                DevHelper.ShowErrorMessageBox("请先选择待分配考生!");
                return;
            }
            foreach (GridRow gr in gridrows)
            {
                userCount++;
                dic.Add(gr.DataRow["SignUpStudentsSpecialityID"].ToString(), strGroupNo + "," + SessionerHelper.ExamLevel + ",Add");
            }
            int iCount_YFP = 0;
            //DataTable dtYFP = service.GetBindGridYFP(strGroupNo, SessionerHelper.SpecialityKindsNO, SessionerHelper.ExamPlaceID, SessionerHelper.ExamLevel);
            //if (dtYFP != null)
            //{
            //    iCount_YFP = dtYFP.Rows.Count;
            //}
            //TB_Sys_ExamPlaceSpecialityKindsGroupMapping model = new RecruitStudents.Client.BLL.BLLAppointment.GetAppointmentGroupModel(strGroupNo);
            //if ((userCount + iCount_YFP) > model.TotalAcount.ToSafeInt())
            //{
            //    DevHelper.ShowErrorMessageBox("选择的人数已经超过当前组最大人数！");
            //    return;
            //}
            //if (dic.Count > 0)
            //{
            //    if (service.UpdateSignUpStudentsSpeciality(dic))
            //    {
            //        BindGrid_YFP();
            //        service.UpdateAppointmentGroupType(strGroupNo, gridView3.Rows.Count);
            //        BindGrid_DFP();
            //        BindGroup();
            //    }
            //}
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (SessionerHelper.ExamLevel == "1")
            {
                DevHelper.ShowErrorMessageBox(SessionerHelper.ExamLevelName + "级别不用进行手动分组!");
                return;
            }
            string strGroupNo = GetGroupNO();
            if (strGroupNo.Length == 0)
            {
                DevHelper.ShowErrorMessageBox("请先选择考试组别!");
                return;
            }
            int userCount = 0;
            Dictionary<string, string> dic = new Dictionary<string, string>();
            GridRowCollection gridrows = gridView3.SelectedRows;
            if (gridrows == null || gridrows.Count == 0)
            {
                DevHelper.ShowErrorMessageBox("请先选择已分配考生!");
                return;
            }
            foreach (GridRow gr in gridrows)
            {
                userCount++;
                dic.Add(gr.DataRow["SignUpStudentsSpecialityID"].ToString(), strGroupNo + "," + SessionerHelper.ExamLevel + ",Remove");
            }
            if (dic.Count > 0)
            {
                //if (service.UpdateSignUpStudentsSpeciality(dic))
                //{
                //    BindGrid_YFP();
                //    service.UpdateAppointmentGroupType(strGroupNo, gridView3.Rows.Count);
                //    BindGrid_DFP();
                //    BindGroup();
                //}
            }
        }

        private void btnExpExcel_Click(object sender, EventArgs e)
        {
            if (SessionerHelper.ExamLevel == "1")
            {
                DevHelper.ShowErrorMessageBox(SessionerHelper.ExamLevelName + "级别不用进行手动分组!");
                return;
            }
            string strGroupName = string.Empty;
            GridView rowsSelected = gridView1 as GridView;
            if (rowsSelected.Rows.Count > 0)
            {
                int[] inteurm = rowsSelected.GetSelectedRows();
                if (inteurm != null && inteurm.Length > 0)
                {
                    strGroupName = rowsSelected.Rows[inteurm[0]].DataRow["GroupName"].ToString();
                }
            }
            BindGrid_YFP();
            DataTable dt = gdAppointmentStudents_YFP.DataSource as DataTable;
            if (dt != null && dt.Rows.Count > 0)
            {
                SaveFileDialog kk = new SaveFileDialog();
                kk.Title = "保存EXECL文件";
                kk.Filter = "EXECL文件(*.xls) |*.xls |所有文件(*.*) |*.*";
                kk.FilterIndex = 1;
                kk.FileName = SessionerHelper.ExamPlaceName + SessionerHelper.SpecialityKinds + "手动分组";
                kk.ShowDialog();
                if (kk.FileName.IndexOf(":") < 0) return; //被点了取消 

                string saveFileName = kk.FileName;
                string title = DateTime.Now.Year.ToSafeString() + "年" + SessionerHelper.ExamPlaceName + SessionerHelper.SpecialityKinds + SessionerHelper.ExamLevelName + "手动分组" + strGroupName;

                //NPOIHelper.ListColumnsName = new SortedList(new NoSort());
                //NPOIHelper.ListColumnsName.Add("Name", "姓名");
                //NPOIHelper.ListColumnsName.Add("SexName", "性别");
                //NPOIHelper.ListColumnsName.Add("IdentityCard", "身份证号");
                //NPOIHelper.ListColumnsName.Add("NMT_SignUpID", "高考报名号");
                //NPOIHelper.ListColumnsName.Add("AllowExamID", "准考证号");
                //NPOIHelper.ListColumnsName.Add("LinkMobile", "手机");
                //NPOIHelper.ListColumnsName.Add("ExamProvinceName", "生源地");
                //NPOIHelper.ExportExcel(dt, saveFileName, title);
            }
            else
            {
                DevHelper.ShowErrorMessageBox("暂时还没有报考专业信息,无法进行导出!");
            }
        }
    }
}
